<template>
  <div id="logTopSearch">
    <h1 class="con-right-title">
      操作日志
    </h1>
    <div class="search-box">
      <el-form
        ref="ruleForm"
        :inline="true"
        :model="formInline"
        :rules="rules"
        class="demo-form-inline"
      >
        <el-form-item label="系统模块" prop="title">
          <el-input
            class="long-input"
            clearable
            v-model="formInline.title"
            placeholder="请输入关键词搜索"
          ></el-input>
        </el-form-item>
        <el-form-item label="操作人员" prop="operUserId">
          <el-select
            v-model="formInline.operUserId"
            filterable
            clearable
            placeholder="请输入关键词"
            class="long"
          >
            <el-option
              v-for="item in operatorsList"
              :key="item.userId"
              :label="item.nickname"
              :value="item.userId"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="操作类型" prop="businessType">
          <el-select
            clearable
            v-model="formInline.businessType"
            placeholder="请选择操作类型"
            class="long"
          >
            <el-option
              v-for="item in typeList"
              :key="item.dictValue"
              :label="item.dictLabel"
              :value="item.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="操作状态" prop="status">
          <el-select
            v-model="formInline.status"
            clearable
            placeholder="请选择操作状态"
          >
            <el-option
              v-for="item in statusList"
              :key="item.dictValue"
              :label="item.dictLabel"
              :value="item.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="操作参数" prop="operParam">
          <el-input
            class="long-input"
            clearable
            v-model="formInline.operParam"
            placeholder="请输入关键词搜索"
          ></el-input>
        </el-form-item>
        <el-form-item label="操作时间" prop="createTime">
          <el-date-picker
            v-model="formInline.createTime"
            type="datetimerange"
            range-separator="至"
            start-placeholder="开始时间"
            end-placeholder="结束时间"
            value-format="yyyy-MM-dd HH:mm:ss"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item class="s-btn">
          <el-button
            type="success"
            icon="el-icon-search"
            @click="toSearch('ruleForm')"
          >
            查询
          </el-button>
          <el-button type="success" plain @click="reset">
            重置
          </el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { sysNormalDisable } from '@utils/dictionariesFunc'
import { getDpNum } from '@/api/system/user'

export default {
  name: 'LogTopSearch',
  msg: '日志管理-顶部搜索',
  props: {
    formInline: {
      type: Object,
      default: function() {
        return {
          title: '', // 系统模块
          operUserId: '', // 操作人员
          businessType: '', // 操业务类型(0=其它,1=新增,2=修改,3=删除,4=授权,5=导出,6=导入,7=强退,8=生成代码,9=清空数据)
          status: '', // 状态0正常,1停用
          startTime: '', // 开始时间
          endTime: '', // 结束时间
          createTime: [],
          operParam: '' // 请求参数
        }
      }
    }
  },
  data() {
    return {
      statusList: [
        // 操作状态
        { dictLabel: '所有', dictValue: '' }
      ],
      typeList: [
        // 操作类型
        { dictLabel: '所有', dictValue: '' }
      ],
      rules: {
        title: {},
        operName: {},
        businessType: {},
        status: {},
        operParam: {},
        createTime: { type: 'array' }
      },
      // 操作人列表
      operatorsList: [{ nickname: '全部', userId: '' }]
    }
  },
  watch: {
    'formInline.createTime': {
      handler(newVal, oldVal) {
        if (newVal && newVal.length > 1) {
          this.formInline.startTime = newVal[0]
          this.formInline.endTime = newVal[1]
        } else {
          this.formInline.startTime = ''
          this.formInline.endTime = ''
        }
      },
      deep: true
    }
  },
  mounted() {
    this.getStatusList()
    this.getOperators()
  },
  methods: {
    /* 搜索 */
    toSearch(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.$emit('toSearch')
        } else {
          return false
        }
      })
    },
    /* 重置 */
    reset() {
      this.$refs['ruleForm'].resetFields()
    },
    /* 获取操作类型 */
    /* 获取用户状态 */
    async getStatusList() {
      // 操作类型
      let data = await sysNormalDisable('sys_oper_type', 'operType')
      this.typeList = this.typeList.concat(data)
      this.$emit('getOperType', this.typeList)
      // 操作状态
      let data2 = await sysNormalDisable('sys_common_status', 'operType')
      this.statusList = this.statusList.concat(data2)
    },
    // 获取操作人员
    getOperators() {
      getDpNum({ type: 'OPERATORS' }, (response) => {
        this.operatorsList = this.operatorsList.concat(response)
      })
    }
  }
}
</script>

<style lang="less">
#logTopSearch {
  .long {
    .el-input.el-input--suffix {
      width: 182px !important;
    }
  }
  .long-input {
    width: 162px !important;
  }
}
</style>
